package android.longs.study.home.servlet.model;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;


import android.longs.study.home.servlet.dao.MobleHomeDao;
import android.longs.study.home.servlet.dao.MobleHomeDaoImple;
import android.longs.study.utils.HBUtils;

public class MobleHomeModelTest {
    MobleHomeDao dao = new MobleHomeDaoImple();


    @Test
    public void add() {

	MobleHomeModel model = new MobleHomeModel();
	model.uid = UUID.randomUUID().toString();
	model.name = "Hibernate bean 对象配制文件综述 ";
	model.createTime = System.currentTimeMillis()+"";
	List<String> arrayList = new ArrayList<String>();
	arrayList.add("配制主键说明 ... ");
	arrayList.add("配制其他属性说明 ... ");
	arrayList.add("多对一配制说明 ... ");

	arrayList.add("级联操作配制 ... ");
	arrayList.add("多对多配制说明 ... ");
	arrayList.add("继承映射配制 ... ... ");

	model.homeDescription=arrayList;

//
//	model.addHtmlPath("wwww5");
//	model.addHtmlPath("23335");
//	model.addHtmlPath("ww2ee3r5");
//	model.addHtmlPath("ttyyu35");
//
//
//	model.addPicturePath("http://c.hiphotos.baidu.com/image/pic/item/78310a55b319ebc4856784ed8126cffc1e1716a2.jpg");
//	model.addPicturePath("http://g.hiphotos.baidu.com/image/pic/item/fd039245d688d43fa9943bf77f1ed21b0ef43bf1.jpg");
//	model.addPicturePath("http://g.hiphotos.baidu.com/image/pic/item/fd039245d688d43fa9943bf77f1ed21b0ef43bf1.jpg");
//	model.addPicturePath("http://g.hiphotos.baidu.com/image/pic/item/fd039245d688d43fa9943bf77f1ed21b0ef43bf1.jpg");
//	model.addPicturePath("http://g.hiphotos.baidu.com/image/pic/item/fd039245d688d43fa9943bf77f1ed21b0ef43bf1.jpg"); 



	HtmlModel htmlModel = new HtmlModel();
	htmlModel.name = "Hibernate Bean对象配制文件详细说明 ";
	htmlModel.path = "http://182.61.44.34:8080/study_android/java/hibernate/hiernate_01.html";

	model.addHtmlModel(htmlModel);


	dao.add(model);

    }
    @Test
    public void aueryAll() {

	List<MobleHomeModel> queryAll = dao.queryAll();

	for (int i = 0; i < queryAll.size(); i++) {
	    MobleHomeModel  mobleHomeModel = queryAll.get(i);
	    System.out.println(mobleHomeModel.toString());
	}



    }

    @Test
    public void aueryFromeName() {
	MobleHomeModel queryFromNameHomeModel = dao.queryFromNameHomeModel("xiao san");
	System.out.println(queryFromNameHomeModel.toString());

    }

    @Test
    public void aueryFromeUid() {
	MobleHomeModel queryFromNameHomeModel = dao.queryFromUidHomeModel("fb8f8c44-f411-4030-a41f-347fdf446392");
	System.out.println(queryFromNameHomeModel.toString());

    }
    @Test
    public void updateModel() {

    }
    @Test
    public void delete(){
	dao.deleteModelFromName("xiao wu 8");
    }


    /**
     * 分页查询 
     */
    @Test
    public void querytestfunction1(){

	System.out.println( " - - - - - ");
	//获取Session 
	Session session = HBUtils.openSession();
	//开启事务 
	Transaction beginTransaction = session.beginTransaction();

	//创建 query 
	//分页查询 ，提供hibernate提供的api分页
	// * 回顾sql  select * from t_user limit startIndex,pageSize;
	// ** startIndex 开始索引
	// ** pageSize 每页显示个数
	Query query = session.createQuery("from MobleHomeModel");
	// 第一页
	//			query.setFirstResult(0);
	//			query.setMaxResults(2);
	// 第二页
	//页页数
	int page = 0;
	//每页的个数
	int num = 20;
	//每页开始查询的开始的位置
	int start = page * num;
	//每页开始的位置  
	query.setFirstResult(start);
	//第页查询的个数
	query.setMaxResults(num);
	//查询  
	List<MobleHomeModel> allUser = query.list();

	System.out.println(allUser.size());
	for (MobleHomeModel user : allUser) {
	    System.out.println("-- --- --- "+user);
	}
    }

    //查询 所有的 
    @Test
    public void queryAllFunction() {
	
	//获取Session 
	Session session = HBUtils.openSession();
	//开启事务 
	Transaction beginTransaction = session.beginTransaction();
	Query query = session.createQuery("from MobleHomeModel");

	//查询  
	List<MobleHomeModel> allUser = query.list();

	System.out.println(allUser.size());
	for (MobleHomeModel user : allUser) {
	    System.out.println("-- --- --- "+user);
	}
	
	beginTransaction.commit();
	session.close();
    }
    
    /**
     * 通过 主键来查询 
     */
    @Test
    public void queryFromIdFunction() {
	System.out.println( " - - - - - ");
	//获取Session 
	Session session = HBUtils.openSession();
	//开启事务 
	Transaction beginTransaction = session.beginTransaction();
	//参数一 要查询的数据封装类
	//参数二 对应数据的主键
	MobleHomeModel model = (MobleHomeModel) session.get(MobleHomeModel.class,1);
	
	System.out.println(model);
	//提交事务
	beginTransaction.commit();
	session.close();
    }
    

    /**
     * 通过 主键来更新数据  
     */
    @Test
    public void queryUpdateIdFunction() {
	System.out.println( " - - - - - ");
	//获取Session 
	Session session = HBUtils.openSession();
	
	//可以设置刷新的时机
	session.setFlushMode(FlushMode.AUTO); //默认 
	//session查询的时候不刷新，事物的提交的时候刷新，执行session.flush方法也刷新
	session.setFlushMode(FlushMode.COMMIT);
	//任何情况下都刷新 
	session.setFlushMode(FlushMode.ALWAYS);
	//只有当执行 session.flush方法时才进行刷新 
	session.setFlushMode(FlushMode.MANUAL);
	//开启事务 
	Transaction beginTransaction = session.beginTransaction();
	
	MobleHomeModel mobleHomeModel = new MobleHomeModel();
	mobleHomeModel.setId(1);
	mobleHomeModel.setName("xx");
	
	//通过主键来更新所有的内容 ，如果没有设置将要被更新为null 
	session.update(mobleHomeModel);

	//提交事务
	beginTransaction.commit();
	session.close();
    }
}


